﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EzDesk.Core;
using EzDesk.Core.CoreImpl;
using EzDesk.Core.Model;

namespace EzDesk.Core.MobileWeb.Utility.Extensions
{
    public static class HtmlHelperExtension
    {
        public static readonly ICoreService _coreSerivce = new CoreService();
        
        public static string NavigationTop(this HtmlHelper html)
        {
            var allApps = _coreSerivce.GetAllApps();

            var htmlBuilder = new StringBuilder();
            // begin
            htmlBuilder.Append("<div id=\"mainnav\" class=\"nav sec\">");
            
            addNavItem("我的EzDesk", "http://" + _coreSerivce.EzDeskUrl + "/", ref htmlBuilder);

            foreach(var app in allApps)
            {
                addNavItem(app.Title, string.Format("http://" + _coreSerivce.EzDeskUrl + "/AppTask/{0}", app.Name), ref htmlBuilder);
            }

            addNavItem("注销", "http://" + _coreSerivce.EzDeskUrl + "/Account/LogOff", ref htmlBuilder, true);
            // end
            htmlBuilder.Append("</div>");

            return htmlBuilder.ToString();
        }

        private static void addNavItem(string text,string href, ref StringBuilder htmlBuilder, bool isLast = false)
        {
         	htmlBuilder.AppendFormat("<a href=\"{0}\">{1}</a>", href, text);
            if (!isLast)
            {
                htmlBuilder.AppendFormat("|");
            }
        }



        public static string SimpleUserLabel(this HtmlHelper html, SimpleUser user, bool displayDept = false)
        {
            // todo: 在SimpleUser加入Dept内容后修改
            if (displayDept)
            {
                return
                    string.Format(
                        "<a href=\"{0}\">{1}</a>(<a href=\"{2}\">{3}</a>)",
                        user.Id.ToString(),
                        user.Name,
                        user.Dept.Id,
                        user.Dept.DeptName
                        );
            }
            else
            {
                return string.Format("<a href=\"#{0}\">{1}</a>", user.Id.ToString(), user.Name);
            }
        }

        public static string SimpleGroupLabel(this HtmlHelper html, Group group)
        {
            return string.Format("<a href=\"#{0}\">{1}</a>", group.Id.ToString(), group.Description);
        }
    }
}
